<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>

</head>
<body>
<div class="x-body">
    <form class="layui-form" id="ReturnForm">

        <div class="layui-form-item">
            <label for="outId" class="layui-form-label">出库编号</label>
            <div class="layui-input-inline">
                <input id="outId" th:value="${reInfo.outId}"  name="outId" required=""
                       autocomplete="off" class="layui-input" readonly="readonly">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="productName" class="layui-form-label">
                <span class="x-red">*</span>产品名称
            </label>
            <div class="layui-input-inline">
                <input type="text" id="productName" name="productName" required="" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="returnDate" class="layui-form-label">退货日期</label>
            <div class="layui-input-inline">
                <input id="returnDate"  name="returnDate" required="" placeholder="退货日期" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="returnQuantity" class="layui-form-label">退货数量</label>
            <div class="layui-input-inline">
                <input type="text" id="returnQuantity" th:value="${reInfo.outQuantity}" name="returnQuantity" required="" lay-verify="returnNum"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="clientName" class="layui-form-label">客户</label>
            <div class="layui-input-inline">
                <select id="clientName" style="width: 150px; height: 25px" name="clientName">
                </select>
            </div>
        </div>

            <div class="layui-form-item">
                <label for="returnReason" class="layui-form-label">退货原因</label>
                <div class="layui-input-inline">
                    <input type="text" id="returnReason" name="returnReason" required="" autocomplete="off" class="layui-input" >
                </div>
            </div>

            <div class="layui-form-item">
                <label for="returnAmount" class="layui-form-label">退货总金额</label>
                <div class="layui-input-inline">
                    <input type="text" id="returnAmount" th:value="${reInfo.outAmount}" name="returnAmount" required=""
                           autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label for="warehouseName" class="layui-form-label">
                    仓库名称
                </label>
                <div class="layui-input-inline">
                    <select id="warehouseName" style="width: 150px;height: 25px" name="warehouseName">
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label for="administrator" class="layui-form-label">
                    仓库管理员
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="administrator" name="administrator" required="" autocomplete="off" class="layui-input" oninput="getAdministrator()">
                </div>
            </div>


        <div class="layui-form-item">
            <label  class="layui-form-label">
            </label>
            <button  class="layui-btn" lay-filter="add" lay-submit="">
                添加
            </button>
        </div>
    </form>
</div>
<script type="text/javascript">
    function getAdministrator() {
        var name = $("#warehouseName").val();
        $.ajax({
            url:"/productReturn/getAdministrator",
            type:"get",
            data:{"name":name},
            dataType:"json",
            success:function (date) {
                document.getElementById("administrator").value = date;
            }
        })
    }
</script>
<script>
    //定义文档加载函数
    $(function(){
        //加载客户
        loadClient();
        //加载仓库名称
        loadWarehouseName();
    })

    //加载客户
    function loadClient() {
        $.ajax({
            url:"/productReturn/clientName",
            async: false,
            type:"get",
            dataType:"json",
            success:function (data) {
                //创建一个拼接字符串的变量
                var html = "";
                html += "<option value=\"--请选择--\">--请选择--</option>";
                //遍历服务端传来的数据集合，就是json数据
                for (var i = 0 ; i < data.length ; i++){
                    html += "<option value='"+data[i].legal_person+"'>"+data[i].legal_person+"</option>";
                }
                //把html加载到下拉框
                $("#clientName").html(html);
            }
        })
    }

    //加载仓库名字
    function loadWarehouseName() {
        $.ajax({
            url:"/productReturn/warehouseName",
            async: false,
            type:"get",
            dataType:"json",
            success:function (data) {
                //创建一个拼接字符串的变量
                var html = "";
                html += "<option value=\"--请选择--\">--请选择--</option>";
                //遍历服务端传来的数据集合，就是json数据
                for (var i = 0 ; i < data.length ; i++){
                    html += "<option value='"+data[i].name+"'>"+data[i].name+"</option>";
                }
                //把html加载到下拉框
                $("#warehouseName").html(html);
            }
        })
    }
    layui.use('laydate', function () {
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#returnDate' //指定元素
        });
    });

    //验证退货数量是否大于入库数量
    function verifyReturnNum() {
        var ID = $("#outId").val();
        var value = $("#returnQuantity").val();
        var result = 0;
        $.ajax({
            url: "/storageNumOver",
            type: "get",
            data: {"outId": ID},
            dataType: "json",//返回的数据类型
            async: false,//必须是同步提交的
            success: function (data) {
                if (data.info > value) {
                    result = 1;
                } else {
                    result = 0;
                }
            },
            error: function () {
                alert("出错了！");
            }
        });
        if (result == 1) {
            return true;
        }
        return false;
    }

    function existWarehousingId(){
        var ID = $("#outId").val();
        var result = 0;
        $.ajax({
            url:"/existWarehousingIdAjax",
            type:"get",
            async: false,
            data:{"outId":ID},
            dataType:"json",//返回的数据类型
            success:function(data){
                //定义{info:"入库编号存在"}
                if(data.info == "入库编号存在"){
                    result = 1;
                }
                else{
                    result = 0;
                }
            }
        })
        if (result == 1) {
            return true;
        }
        return false;
    }

    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form
            ,layer = layui.layer;

        //修改退货表单
        //自定义验证规则
        form.verify({
            //退货数量必须为整数
            returnNum: function(value) {
                //创建一个正则表达式对象
                var re = /^[0-9]*$/;
                //验证用户输入的入库数量是否是数字
                if (!re.test(value)) {
                    return '退货数量必须为数字！';
                }
                if (!verifyReturnNum()) {
                    return '退货数量必须小于入库数量';
                }
            },
            //入库编号必须存在
            outId:function (value) {
                if(!existWarehousingId()){
                    return '入库编号不存在';
                }
            }

        });

        //添加
        form.on('submit(add)', function(data){
            console.log(data.field);
            $.ajax({
                url:"/addReturnAjax",
                type:"post",
                data:data.field,
                dataType:"json",//返回的数据类型
                success:function(data){
                    //定义{info:"添加成功"}
                    $("#info").text(data);
                    if(data.info == "添加成功"){
                        layer.alert("添加成功",function(index) {
                            //关闭弹窗操作
                            //获取Frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭弹窗
                            parent.layer.close(index);
                            //刷新
                            parent.window.location.href = parent.window.location.href;
                        });
                    }else{
                        layer.alert("添加失败");
                    }
                }
            })
            return false;
        });
    });
</script>
</body>
</html>